function reactive (target) {
  return new Proxy(target, {
    get (target, key, receiver) {
      track(target, key) // 依赖追踪
      return Reflect.get(target, key)
    },

    set (target, key, value, receiver) {
      const flag = Reflect.set(target, key, value)
      trigger(target, key) // 触发
      return flag
    }
  })
}

const obj = reactive({
  a: 1
})


const targetMap = new WeakMap()
let activeEffect

function effect (fn) {
  const effectFn = function () {
    activeEffect = effectFn
    fn()
  }
  effectFn()
  activeEffect = undefined
}

function track (target, key) {
  if (!activeEffect) return

  let depsMap = targetMap.get(target)
  if (!depsMap) {
    targetMap.set(target, (depsMap = new Map()))
  }

  let deps = depsMap.get(key)
  if (!deps) {
    depsMap.set(key, (deps = new Set()))
  }

  deps.add(activeEffect)
}

function trigger (target, key) {
  const depsMap = targetMap.get(target)

  if (!depsMap) return

  const effects = depsMap.get(key)
  effects && effects.forEach(effectFn => effectFn())
}

effect(() => {
  console.log(obj.a, '属性a被修改了')
})

obj.a = 2